package org.aphelie.romancer.ui.wizards;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

public class DocumentNewFilePage extends WizardNewFileCreationPage {

	public DocumentNewFilePage(String pageName, IStructuredSelection selection) {
		super(pageName, selection);
		this.setTitle("New File Titre");
	}
	
	public IFile getFile() {
		IPath path = getContainerFullPath();
		String fileName = getFileName();
		return ResourcesPlugin.getWorkspace().getRoot().getFile(path.append(fileName));
	}
	
	@Override
	protected boolean validatePage() {
		return super.validatePage() && validateFileExtension();
	}

	private boolean validateFileExtension() {
		String fileExtension = (getContainerFullPath().append(
				getFileName())).getFileExtension();
		if (fileExtension == null) {
			return true;
		}
		String[] extensions = {"txt", "atk"};
		for (String extension : extensions) {
			if (fileExtension.equals(extension)) {
				return true;
			}
		}
		setErrorMessage("Only .txt is accepted");
		
		return false;
	}

}
